﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AForge.Math;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Controls;
using System.IO;
using System.Drawing.Imaging;
using AForge.Imaging.Filters;

namespace FingerVein_Database_Collection
{
    public partial class MainForm : Form
    {
        FingerVeinDBModelContainer dbmodel = new FingerVeinDBModelContainer();

        View_Captured_Data_Form vdata;

        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoDevice;
        private VideoCapabilities[] videoCapabilities;

        private bool capture;

        public static BrightnessCorrection brightnessFilter = new BrightnessCorrection(0);
        public static ContrastCorrection contrastFilter = new ContrastCorrection(0);
        public static SaturationCorrection saturationFilter = new SaturationCorrection(0.0f);
        public static GaussianSharpen gausiapSharpenfilter = new GaussianSharpen();
        public static GammaCorrection gammaFilter = new GammaCorrection(1.0f);

        public Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);

        public static string UserID;
        public static string UserName;
        public static string UserGender;
        public static string UserEmail;
        public static string UserAge;
        public static string UserStatus;
        public static string UserNationality;

        public static Bitmap[] Left_Index = new Bitmap[4];
        public static Bitmap[] Left_Middle = new Bitmap[4];
        public static Bitmap[] Left_Ring = new Bitmap[4];

        public static Bitmap[] Right_Index = new Bitmap[4];
        public static Bitmap[] Right_Middle = new Bitmap[4];
        public static Bitmap[] Right_Ring = new Bitmap[4];

        //public int[] Left_Finger_Index_Status = new int[6];
        //public int[] Left_Finger_Middle_Status = new int[6];
        //public int[] Left_Finger_Ring_Status = new int[6];

        //public int[] Right_Finger_Index_Status = new int[6];
        //public int[] Right_Finger_Middle_Status = new int[6];
        //public int[] Right_Finger_Ring_Status = new int[6];


        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count != 0)
            {
                foreach (FilterInfo device in videoDevices)
                {
                    devicesCombo.Items.Add(device.Name);
                }
            }
            else
            {
                devicesCombo.Items.Add("No DirectShow devices found");
            }

            //User_Information_Form usrInfo = new User_Information_Form(this);
            //usrInfo.ShowDialog();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Disconnect();
        }

        private void devicesCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (videoDevices.Count != 0)
            {
                videoDevice = new VideoCaptureDevice(videoDevices[devicesCombo.SelectedIndex].MonikerString);
                EnumeratedSupportedFrameSizes(videoDevice);
            }
        }

        private void EnumeratedSupportedFrameSizes(VideoCaptureDevice videoDevice)
        {
            this.Cursor = Cursors.WaitCursor;

            videoResolutionsCombo.Items.Clear();

            try
            {
                videoCapabilities = videoDevice.VideoCapabilities;

                foreach (VideoCapabilities capabilty in videoCapabilities)
                {
                    if (!videoResolutionsCombo.Items.Contains(capabilty.FrameSize))
                    {
                        videoResolutionsCombo.Items.Add(capabilty.FrameSize);
                    }
                }

                if (videoCapabilities.Length == 0)
                {
                    videoResolutionsCombo.Items.Add("Not supported");
                }

                videoResolutionsCombo.SelectedIndex = 0;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void connectButton_Click(object sender, EventArgs e)
        {
            if (videoDevice != null)
            {
                if ((videoCapabilities != null) && (videoCapabilities.Length != 0))
                {
                    videoDevice.DesiredFrameSize = (Size)videoResolutionsCombo.SelectedItem;
                }

                videoDevice.DesiredFrameRate = 24;
                videoSourcePlayer.VideoSource = videoDevice;
                videoSourcePlayer.Start();

                videoDevice.NewFrame += new NewFrameEventHandler(videoSource_newFrame);
            }
        }
        int tx = 10, ty = 20, twidth = 200, theight = 100;
        public void videoSource_newFrame(object sender, NewFrameEventArgs eventArgs)
        {

            Bitmap captureFrame = null;

            captureFrame = (Bitmap)eventArgs.Frame.Clone();

            Bitmap grayImage = grayscaleFilter.Apply(captureFrame);
          //  brightnessFilter.ApplyInPlace(captureFrame);
          //  contrastFilter.ApplyInPlace(captureFrame);
          //  saturationFilter.ApplyInPlace(captureFrame);
          ////  gausiapSharpenfilter.ApplyInPlace(captureFrame);
          //  gammaFilter.ApplyInPlace(captureFrame);


            Bitmap source = new Bitmap(grayImage);
            Bitmap CroppedImage = source.Clone(new System.Drawing.Rectangle(tx, ty, twidth, theight), source.PixelFormat);

            AForge.Imaging.ImageStatistics statistics = new AForge.Imaging.ImageStatistics(CroppedImage);
            AForge.Math.Histogram histogram = statistics.Red;

            double test = histogram.StdDev;

            //if (textBox1.InvokeRequired)
            //{
            //    textBox1.Invoke(new MethodInvoker(delegate { textBox1.Text = test.ToString(); }));
            //}

            //textBox1.Text = test.ToString();

            //pictureBox1.Image = (Bitmap)CroppedImage;
            if (capture == false)
            {
                pbCapturedImage.Image = (Bitmap)captureFrame.Clone();
            }

        }



        private void disconnectButton_Click(object sender, EventArgs e)
        {
            Disconnect();
        }

        private void Disconnect()
        {
            if (videoSourcePlayer.VideoSource != null)
            {
                // stop video device
                videoSourcePlayer.SignalToStop();
                videoSourcePlayer.WaitForStop();
                videoSourcePlayer.VideoSource = null;
            }
        }


        private void btnClear_Click(object sender, EventArgs e)
        {
            //var checkedButton = gbFingers.Controls.OfType<RadioButton>()
            //                          .FirstOrDefault(r => r.Checked);
            //if (checkedButton == null)
            //{
            //    MessageBox.Show("Please select a finger to clear");
            //}
            //else
            //{
            //    //rbInfo[1] = right or left
            //    //rbInfo[2] = finger number
            //    string[] rbInfo = checkedButton.Name.Split('_');

            //    string RightorLeft = rbInfo[1];

            //    string inputString = rbInfo[2];

            //    string FingerType = inputString.Substring(0, 1);
            //    string FingerNo = inputString.Substring(inputString.Length - 1);

            //    if (RightorLeft == "L" && FingerType == "I")
            //    {
            //        Left_Index[Convert.ToInt16(FingerNo) - 1] = null;
            //        checkedButton.ForeColor = Color.Red;
            //    }
            //    else if (RightorLeft == "L" && FingerType == "M")
            //    {
            //        Left_Middle[Convert.ToInt16(FingerNo) - 1] = null;
            //        checkedButton.ForeColor = Color.Red;
            //    }
            //    else if (RightorLeft == "L" && FingerType == "R")
            //    {
            //        Left_Ring[Convert.ToInt16(FingerNo) - 1] = null;
            //        checkedButton.ForeColor = Color.Red;
            //    }
            //    else if (RightorLeft == "R" && FingerType == "I")
            //    {
            //        Right_Index[Convert.ToInt16(FingerNo) - 1] = null;
            //        checkedButton.ForeColor = Color.Red;
            //    }
            //    else if (RightorLeft == "R" && FingerType == "M")
            //    {
            //        Right_Middle[Convert.ToInt16(FingerNo) - 1] = null;
            //        checkedButton.ForeColor = Color.Red;
            //    }
            //    else if (RightorLeft == "R" && FingerType == "R")
            //    {
            //        Right_Middle[Convert.ToInt16(FingerNo) - 1] = null;
            //        checkedButton.ForeColor = Color.Red;
            //    }
            //}

            capture = false;
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            var checkedButton = gbFingers.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
            if (checkedButton == null)
            {
                MessageBox.Show("Please select a finger to enroll");
            }
            else if (pbCapturedImage.Image == null)
            {
                MessageBox.Show("No Image to capture");
            }
            else
            {
                string[] rightOrleft = checkedButton.Name.Split('_');
                saveFileDialog.FileName = UserID + "_" + rightOrleft[1] + "_" + checkedButton.Text;
                saveFileDialog.DefaultExt = "png";
                saveFileDialog.ValidateNames = true;

                // saveFileDialog.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif|JPEG Image (.jpeg)|*.jpeg|Png Image (.png)|*.png|Tiff Image (.tiff)|*.tiff|Wmf Image (.wmf)|*.wmf";
                saveFileDialog.Filter = "Png Image (.png)|*.png";
               // Bitmap bitmap = new Bitmap(pbCapturedImage.Image);

                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    int width = Convert.ToInt32(pbCapturedImage.Width);
                    int height = Convert.ToInt32(pbCapturedImage.Height);
                    Bitmap bmp = new Bitmap(width, height);
                    pbCapturedImage.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));

                    string ext = Path.GetExtension(saveFileDialog.FileName).ToLower();
                    ImageFormat imgFormat = ImageFormat.Png;

                    bmp.Save(saveFileDialog.FileName, imgFormat);
                }
            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            if (videoDevice != null)
            {
                videoDevice.DisplayPropertyPage(IntPtr.Zero);
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Settings_Form stf = new Settings_Form();
            stf.ShowDialog();
        }

        private void btnsaveJPG_Click(object sender, EventArgs e)
        {
            var checkedButton = gbFingers.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
            if (checkedButton == null)
            {
                MessageBox.Show("Please select a finger to enroll");
            }
            else if(pbCapturedImage.Image == null)
            {
                MessageBox.Show("No Image to capture");
            }
            else
            {
                string[] rightOrleft = checkedButton.Name.Split('_');
                saveFileDialog.FileName = UserID + "_" + rightOrleft[1] + "_" + checkedButton.Text;
                saveFileDialog.DefaultExt = "jpg";
                saveFileDialog.ValidateNames = true;

                // saveFileDialog.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif|JPEG Image (.jpeg)|*.jpeg|Png Image (.png)|*.png|Tiff Image (.tiff)|*.tiff|Wmf Image (.wmf)|*.wmf";
                saveFileDialog.Filter = "JPEG Image (.jpeg)|*.jpeg";
              //  Bitmap bitmap = new Bitmap(pbCapturedImage.Image);

                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    int width = Convert.ToInt32(pbCapturedImage.Width);
                    int height = Convert.ToInt32(pbCapturedImage.Height);
                    Bitmap bmp = new Bitmap(width, height);
                    pbCapturedImage.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));

                    string ext = Path.GetExtension(saveFileDialog.FileName).ToLower();
                    ImageFormat imgFormat = ImageFormat.Jpeg;

                    bmp.Save(saveFileDialog.FileName, imgFormat);
                }
            }
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            var ImageCaptured = gbFingers.Controls.OfType<RadioButton>().Count(r => r.ForeColor == Color.Red);

            if (ImageCaptured == 0 && MainForm.UserID != null)
            {
                Disconnect();

                //save data to database
                int userid = Convert.ToInt32(UserID);

                var idcheck = from x in dbmodel.USER_TABLE
                              where x.User_ID == userid
                              select x;

                if (idcheck.Count() == 0)
                {

                    USER_TABLE usr = new USER_TABLE();

                    usr.User_ID = userid;
                    usr.User_Name = UserName;
                    usr.User_Gender = UserGender;
                    usr.User_Email = UserEmail;
                    usr.User_Age = UserAge;
                    usr.User_Status = UserStatus;
                    usr.User_Nationality = UserNationality;

                    dbmodel.AddToUSER_TABLE(usr);
                    dbmodel.SaveChanges();

                    USER_FINGERS_TABLE usrFgr = new USER_FINGERS_TABLE();

                    for (int i = 0; i < Left_Index.Count(); i++)
                    {
                        usrFgr.User_ID = usr.User_ID;
                        usrFgr.User_Hand = "Left";
                        usrFgr.Finger_Name = "Index";
                        usrFgr.Finger_No = (i + 1).ToString();
                        usrFgr.Finger_Data = ImageByteConv.ImagetoByte(Left_Index[i]);

                        dbmodel.AddToUSER_FINGERS_TABLE(usrFgr);
                    }

                    for (int i = 0; i < Left_Middle.Count(); i++)
                    {
                        usrFgr.User_ID = usr.User_ID;
                        usrFgr.User_Hand = "Left";
                        usrFgr.Finger_Name = "Middle";
                        usrFgr.Finger_No = (i + 1).ToString();
                        usrFgr.Finger_Data = ImageByteConv.ImagetoByte(Left_Middle[i]);

                        dbmodel.AddToUSER_FINGERS_TABLE(usrFgr);
                    }

                    for (int i = 0; i < Left_Ring.Count(); i++)
                    {
                        usrFgr.User_ID = usr.User_ID;
                        usrFgr.User_Hand = "Left";
                        usrFgr.Finger_Name = "Ring";
                        usrFgr.Finger_No = (i + 1).ToString();
                        usrFgr.Finger_Data = ImageByteConv.ImagetoByte(Left_Ring[i]);

                        dbmodel.AddToUSER_FINGERS_TABLE(usrFgr);
                    }

                    for (int i = 0; i < Right_Index.Count(); i++)
                    {
                        usrFgr.User_ID = usr.User_ID;
                        usrFgr.User_Hand = "Left";
                        usrFgr.Finger_Name = "Index";
                        usrFgr.Finger_No = (i + 1).ToString();
                        usrFgr.Finger_Data = ImageByteConv.ImagetoByte(Right_Index[i]);

                        dbmodel.AddToUSER_FINGERS_TABLE(usrFgr);
                    }

                    for (int i = 0; i < Right_Middle.Count(); i++)
                    {
                        usrFgr.User_ID = usr.User_ID;
                        usrFgr.User_Hand = "Left";
                        usrFgr.Finger_Name = "Middle";
                        usrFgr.Finger_No = (i + 1).ToString();
                        usrFgr.Finger_Data = ImageByteConv.ImagetoByte(Right_Middle[i]);

                        dbmodel.AddToUSER_FINGERS_TABLE(usrFgr);
                    }

                    for (int i = 0; i < Right_Ring.Count(); i++)
                    {
                        usrFgr.User_ID = usr.User_ID;
                        usrFgr.User_Hand = "Left";
                        usrFgr.Finger_Name = "Ring";
                        usrFgr.Finger_No = (i + 1).ToString();
                        usrFgr.Finger_Data = ImageByteConv.ImagetoByte(Right_Ring[i]);

                        dbmodel.AddToUSER_FINGERS_TABLE(usrFgr);
                    }

                    dbmodel.SaveChanges();

                    MessageBox.Show("User data collection completed");

                    UserID = null;
                    UserName = null;
                    UserEmail = null;
                    UserAge = null;
                    UserStatus = null;
                    UserNationality = null;

                    pbCapturedImage.Image = null;

                    Left_Index = new Bitmap[4];
                    Left_Middle = new Bitmap[4];
                    Left_Ring = new Bitmap[4];

                    Right_Index = new Bitmap[4];
                    Right_Middle = new Bitmap[4];
                    Right_Ring = new Bitmap[4];

                    //clear all the selected radio buttion
                    gbFingers.Controls.OfType<RadioButton>().ToList().ForEach(p => p.Checked = false);

                    RefreshRb();

                    User_Information_Form newUserInfo = new User_Information_Form(this);
                    newUserInfo.ShowDialog();
                }
                else
                {
                    DialogResult dialogResult = MessageBox.Show("User ID is already exist, Do you want to update user data?", "Message", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        int updateUserID = userid;

                        idcheck.First().User_ID = Convert.ToInt32(UserID);
                        idcheck.First().User_Name = UserName;
                        idcheck.First().User_Gender = UserGender;
                        idcheck.First().User_Email = UserEmail;
                        idcheck.First().User_Age = UserAge;
                        idcheck.First().User_Status = UserStatus;
                        idcheck.First().User_Nationality = UserNationality;

                        dbmodel.SaveChanges();

                        var userfingerdata = from x in dbmodel.USER_FINGERS_TABLE
                                             where x.User_ID == updateUserID
                                             select x;


                        for (int i = 0; i < Left_Index.Count(); i++)
                        {
                            var fgr = from x in dbmodel.USER_FINGERS_TABLE
                                      where x.User_ID == updateUserID && x.User_Hand == "Left" && x.Finger_Name == "Index" && x.Finger_No == (i + 1).ToString()
                                      select x;

                            fgr.FirstOrDefault().User_ID = Convert.ToInt32(UserID);
                            fgr.FirstOrDefault().User_Hand = "Left";
                            fgr.FirstOrDefault().Finger_Name = "Index";
                            fgr.FirstOrDefault().Finger_No = (i + 1).ToString();
                            fgr.FirstOrDefault().Finger_Data = ImageByteConv.ImagetoByte(Left_Index[i]);
                        }
                        for (int i = 0; i < Left_Middle.Count(); i++)
                        {
                            var fgr = from x in dbmodel.USER_FINGERS_TABLE
                                      where x.User_ID == updateUserID && x.User_Hand == "Left" && x.Finger_Name == "Middle" && x.Finger_No == (i + 1).ToString()
                                      select x;

                            fgr.FirstOrDefault().User_ID = Convert.ToInt32(UserID);
                            fgr.FirstOrDefault().User_Hand = "Left";
                            fgr.FirstOrDefault().Finger_Name = "Middle";
                            fgr.FirstOrDefault().Finger_No = (i + 1).ToString();
                            fgr.FirstOrDefault().Finger_Data = ImageByteConv.ImagetoByte(Left_Middle[i]);
                        }
                        for (int i = 0; i < Left_Ring.Count(); i++)
                        {
                            var fgr = from x in dbmodel.USER_FINGERS_TABLE
                                      where x.User_ID == updateUserID && x.User_Hand == "Left" && x.Finger_Name == "Ring" && x.Finger_No == (i + 1).ToString()
                                      select x;

                            fgr.FirstOrDefault().User_ID = Convert.ToInt32(UserID);
                            fgr.FirstOrDefault().User_Hand = "Left";
                            fgr.FirstOrDefault().Finger_Name = "Ring";
                            fgr.FirstOrDefault().Finger_No = (i + 1).ToString();
                            fgr.FirstOrDefault().Finger_Data = ImageByteConv.ImagetoByte(Left_Ring[i]);
                        }

                        for (int i = 0; i < Right_Index.Count(); i++)
                        {
                            var fgr = from x in dbmodel.USER_FINGERS_TABLE
                                      where x.User_ID == updateUserID && x.User_Hand == "Right" && x.Finger_Name == "Index" && x.Finger_No == (i + 1).ToString()
                                      select x;

                            fgr.FirstOrDefault().User_ID = Convert.ToInt32(UserID);
                            fgr.FirstOrDefault().User_Hand = "Right";
                            fgr.FirstOrDefault().Finger_Name = "Index";
                            fgr.FirstOrDefault().Finger_No = (i + 1).ToString();
                            fgr.FirstOrDefault().Finger_Data = ImageByteConv.ImagetoByte(Right_Index[i]);
                        }
                        for (int i = 0; i < Right_Middle.Count(); i++)
                        {
                            var fgr = from x in dbmodel.USER_FINGERS_TABLE
                                      where x.User_ID == updateUserID && x.User_Hand == "Right" && x.Finger_Name == "Middle" && x.Finger_No == (i + 1).ToString()
                                      select x;

                            fgr.FirstOrDefault().User_ID = Convert.ToInt32(UserID);
                            fgr.FirstOrDefault().User_Hand = "Right";
                            fgr.FirstOrDefault().Finger_Name = "Middle";
                            fgr.FirstOrDefault().Finger_No = (i + 1).ToString();
                            fgr.FirstOrDefault().Finger_Data = ImageByteConv.ImagetoByte(Right_Middle[i]);
                        }
                        for (int i = 0; i < Right_Ring.Count(); i++)
                        {
                            var fgr = from x in dbmodel.USER_FINGERS_TABLE
                                      where x.User_ID == updateUserID && x.User_Hand == "Right" && x.Finger_Name == "Ring" && x.Finger_No == (i + 1).ToString()
                                      select x;

                            fgr.FirstOrDefault().User_ID = Convert.ToInt32(UserID);
                            fgr.FirstOrDefault().User_Hand = "Right";
                            fgr.FirstOrDefault().Finger_Name = "Ring";
                            fgr.FirstOrDefault().Finger_No = (i + 1).ToString();
                            fgr.FirstOrDefault().Finger_Data = ImageByteConv.ImagetoByte(Right_Ring[i]);
                        }
                        dbmodel.SaveChanges();

                    }
                }
            }


            else
            {
                MessageBox.Show("Please fill in all the user information and collect all the data");
            }
        }

        private void btnViewUserInfos_Click(object sender, EventArgs e)
        {
            User_Information_Form viewUserInfo = new User_Information_Form(this);

            //viewUserInfo.txtUserID.Enabled = false;
            //viewUserInfo.txtName.Enabled = false;
            //viewUserInfo.txtEmail.Enabled = false;
            //viewUserInfo.txtAge.Enabled = false;
            //viewUserInfo.txtStatus.Enabled = false;
            //viewUserInfo.rbFemale.Enabled = false;
            //viewUserInfo.rbMale.Enabled = false;
            //viewUserInfo.txtNationality.Enabled = false;

            viewUserInfo.txtUserID.Text = UserID;
            viewUserInfo.txtName.Text = UserName;
            if (UserGender == "Male")
            {
                viewUserInfo.rbMale.Checked = true;
            }
            else
            {
                viewUserInfo.rbFemale.Checked = true;
            }
            viewUserInfo.txtEmail.Text = UserEmail;
            viewUserInfo.txtAge.Text = UserAge;
            viewUserInfo.txtStatus.Text = UserStatus;
            viewUserInfo.txtNationality.Text = UserNationality;

            //viewUserInfo.btnDone.Text = "OK";
            //viewUserInfo.btnCancel.Visible = false;

            viewUserInfo.ShowDialog();

        }

        private void btnViewCapturedData_Click(object sender, EventArgs e)
        {
            vdata = new View_Captured_Data_Form();
            vdata.ShowDialog();

            RefreshRb();
        }

        private void btnEnrollnewUser_Click(object sender, EventArgs e)
        {
            //put yes and no messagebox

            DialogResult dialogResult = MessageBox.Show("All the data collected will be clear, are you sure to enroll new user?", "Enroll new user", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                Disconnect();

                UserID = null;
                UserName = null;
                UserEmail = null;
                UserAge = null;
                UserStatus = null;
                UserNationality = null;

                pbCapturedImage.Image = null;

                Left_Index = new Bitmap[4];
                Left_Middle = new Bitmap[4];
                Left_Ring = new Bitmap[4];

                Right_Index = new Bitmap[4];
                Right_Middle = new Bitmap[4];
                Right_Ring = new Bitmap[4];

                //clear all the selected radio buttion
                gbFingers.Controls.OfType<RadioButton>().ToList().ForEach(p => p.Checked = false);

                RefreshRb();

                User_Information_Form newUserInfo = new User_Information_Form(this);
                newUserInfo.ShowDialog();
            }
            else if (dialogResult == DialogResult.No)
            {
                //do something else
            }
        }
        

        private void RefreshRb()
        {
            RadioButton[] rb_Left_Index_Array = new RadioButton[] { rb_L_I1, rb_L_I2, rb_L_I3, rb_L_I4,   };
            RadioButton[] rb_Left_Middle_Array = new RadioButton[] { rb_L_M1, rb_L_M2, rb_L_M3, rb_L_M4,   };
            RadioButton[] rb_Left_Ring_Array = new RadioButton[] { rb_L_R1, rb_L_R2, rb_L_R3, rb_L_R4,   };

            RadioButton[] rb_Right_Index_Array = new RadioButton[] { rb_R_I1, rb_R_I2, rb_R_I3, rb_R_I4,   };
            RadioButton[] rb_Right_Middle_Array = new RadioButton[] { rb_R_M1, rb_R_M2, rb_R_M3, rb_R_M4,   };
            RadioButton[] rb_Right_Ring_Array = new RadioButton[] { rb_R_R1, rb_R_R2, rb_R_R3, rb_R_R4,   };


            for (int i = 0; i < 4; i++)
            {
                if (Left_Index[i] != null)
                {
                    rb_Left_Index_Array[i].ForeColor = Color.Green;
                }
                else
                { rb_Left_Index_Array[i].ForeColor = Color.Red; }

                 if (Left_Middle[i] != null)
                {
                    rb_Left_Middle_Array[i].ForeColor = Color.Green;
                }
                 else
                 { rb_Left_Middle_Array[i].ForeColor = Color.Red; }

                 if (Left_Ring[i] != null)
                {
                    rb_Left_Ring_Array[i].ForeColor = Color.Green;
                }
                 else
                 { rb_Left_Ring_Array[i].ForeColor = Color.Red; }

                 if (Right_Index[i] != null)
                {
                    rb_Right_Index_Array[i].ForeColor = Color.Green;
                }
                 else
                 { rb_Right_Index_Array[i].ForeColor = Color.Red; }

                 if (Right_Middle[i] != null)
                {
                    rb_Right_Middle_Array[i].ForeColor = Color.Green;
                }
                 else
                 { rb_Right_Middle_Array[i].ForeColor = Color.Red; }

                 if (Right_Ring[i] != null)
                {
                    rb_Right_Ring_Array[i].ForeColor = Color.Green;
                }
                else
                 { rb_Right_Ring_Array[i].ForeColor = Color.Red; }

            }

        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            capture = true;
        }

        private void btnCapture_Click(object sender, EventArgs e)
        {
            var checkedButton = gbFingers.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
            if (checkedButton == null)
            {
                MessageBox.Show("Please select a finger for Save");
            }
            else
            {
                //rbInfo[1] = right or left
                //rbInfo[2] = finger number
                string[] rbInfo = checkedButton.Name.Split('_');

                string RightorLeft = rbInfo[1];

                string inputString = rbInfo[2];

                string FingerType = inputString.Substring(0, 1);
                string FingerNo = inputString.Substring(inputString.Length - 1);

                if (RightorLeft == "L" && FingerType == "I")
                {
                    Left_Index[Convert.ToInt16(FingerNo) - 1] = (Bitmap)pbCapturedImage.Image;
                    //   checkedButton.ForeColor = Color.Green;
                }
                else if (RightorLeft == "L" && FingerType == "M")
                {
                    Left_Middle[Convert.ToInt16(FingerNo) - 1] = (Bitmap)pbCapturedImage.Image;
                    //   checkedButton.ForeColor = Color.Green;
                }
                else if (RightorLeft == "L" && FingerType == "R")
                {
                    Left_Ring[Convert.ToInt16(FingerNo) - 1] = (Bitmap)pbCapturedImage.Image;
                    //    checkedButton.ForeColor = Color.Green;
                }
                else if (RightorLeft == "R" && FingerType == "I")
                {
                    Right_Index[Convert.ToInt16(FingerNo) - 1] = (Bitmap)pbCapturedImage.Image;
                    //   checkedButton.ForeColor = Color.Green;
                }
                else if (RightorLeft == "R" && FingerType == "M")
                {
                    Right_Middle[Convert.ToInt16(FingerNo) - 1] = (Bitmap)pbCapturedImage.Image;
                    //   checkedButton.ForeColor = Color.Green;
                }
                else if (RightorLeft == "R" && FingerType == "R")
                {
                    Right_Ring[Convert.ToInt16(FingerNo) - 1] = (Bitmap)pbCapturedImage.Image;
                    //   checkedButton.ForeColor = Color.Green;
                }

                RefreshRb();

            }
        }

        private void btnSaveasBMP_Click(object sender, EventArgs e)
        {
            var checkedButton = gbFingers.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(r => r.Checked);
            if (checkedButton == null)
            {
                MessageBox.Show("Please select a finger to enroll");
            }
            else if (pbCapturedImage.Image == null)
            {
                MessageBox.Show("No Image to capture");
            }
            else
            {
                string[] rightOrleft = checkedButton.Name.Split('_');
                saveFileDialog.FileName = UserID + "_" + rightOrleft[1] + "_" + checkedButton.Text;
                saveFileDialog.DefaultExt = "bmp";
                saveFileDialog.ValidateNames = true;

                // saveFileDialog.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif|JPEG Image (.jpeg)|*.jpeg|Png Image (.png)|*.png|Tiff Image (.tiff)|*.tiff|Wmf Image (.wmf)|*.wmf";
                saveFileDialog.Filter = "BMP Image (.bmp)|*.bmp";
                //  Bitmap bitmap = new Bitmap(pbCapturedImage.Image);

                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    int width = Convert.ToInt32(pbCapturedImage.Width);
                    int height = Convert.ToInt32(pbCapturedImage.Height);
                    Bitmap bmp = new Bitmap(width, height);
                    pbCapturedImage.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));

                    string ext = Path.GetExtension(saveFileDialog.FileName).ToLower();
                    ImageFormat imgFormat = ImageFormat.Bmp;

                    bmp.Save(saveFileDialog.FileName, imgFormat);
                }
            }
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
           

        
    }
}
